package top.codedance.iotp.client.plugins.sync.http;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.client.net.Httpd;
import top.codedance.iotp.common.executer.SyncAbstractExecuter;
import top.codedance.iotp.common.entity.PluginResultEntity;

import java.nio.charset.StandardCharsets;

public class PushDataExecuterImpl extends SyncAbstractExecuter {
    @Override
    public PluginResultEntity exec(byte[] data, String taskId) {
        PluginResultEntity result = new PluginResultEntity();
        try {
            String str = new String(data, StandardCharsets.UTF_8);
            JSONObject client = JSONObject.parseObject(str);
            Integer clientId = client.getInteger("id");
            result.setOk(true);
            result.setContext(new String(Httpd.getData(clientId), StandardCharsets.UTF_8));
        }catch (Exception ex){
            result.setOk(false);
            result.setMsg(ex.getMessage());
        }
        return result;
    }
}
